﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using KNet.Data.Entity;
using KNet.Foundation.DRS.Model;
using CNKI.TPI.Web.Search.Model;
using KNet.Data.KBaseClient;

namespace CNKI.TPI.Web.Base
{
    public interface IDataContext
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        bool Insert<T>(T model, string tableName = null, string ctxName = null) where T : new();

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        bool Update<T>(T model, string tableName = null, string ctxName = null) where T : new();

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="match"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        bool Delete<T>(Expression<Func<T, bool>> match, string tableName = null, string ctxName = null) where T : new();

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        bool Delete<T>(T model, string tableName = null, string ctxName = null) where T : new();

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="match"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        IList<T> Get<T>(Expression<Func<T, bool>> match, Expression<Func<T, bool>> order = null, string ctxName = null, int dataCacheTimeout = 0) where T : BaseModel, IView, new();

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="match"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="order"></param>
        /// <param name="ctxName"></param>
        /// <returns></returns>
        IList<T> Get<T>(Expression<Func<T, bool>> match, int pageIndex, int pageSize, out int count, Expression<Func<T, bool>> order = null, string ctxName = null, int dataCacheTimeout = 0) where T : BaseModel, IView, new();

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="order"></param>
        /// <returns></returns>
        IList<T> GetAll<T>(Expression<Func<T, bool>> order = null, string ctxName = null, int dataCacheTimeout = 0) where T : BaseModel, IView, new();

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="order"></param>
        /// <param name="ctxName"></param>
        /// <returns></returns>
        IList<T> GetAll<T>(int pageIndex, int pageSize, out int count, Expression<Func<T, bool>> order = null, string ctxName = null, int dataCacheTimeout = 0) where T : BaseModel, IView, new();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        bool ExecuteNonQuery(string sql, string ctxName = null);

        int ExecuteSqlNonQuery(string sql, string ctxName = null);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        DataTable ExecuteSql(string sql, string ctxName = null, int maxLength = 512, int dataCacheTimeout = 0);

        DataTable ExecuteSql(string sql, int pageIndex, int pageCount, out int count, bool isUnicode = false, string ctxName = null, int dataCacheTimeout = 0, string connectStr = null);

        DataSet ExecuteSql(string sql, IList<string> tableList, int pageIndex, int pageCount, bool isUnicode = false, string ctxName = null, int dataCacheTimeout = 0);

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <returns></returns>
        IList<T> ExecuteSql<T>(string sql, string ctxName = null, int maxLength = -1, int dataCacheTimeout = 0) where T : BaseModel, IView, new();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="fields"></param>
        /// <param name="query"></param>
        /// <param name="group"></param>
        /// <param name="order"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        DataSet QueryByDRS(string type, string fields, string query, string group, string order, int start, int length);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="drsName"></param>
        /// <param name="resource"></param>
        /// <returns></returns>
        OperateResult AppendDbResource(string drsName, DbResource resource, decimal weight, string group);


        /// <summary>
        /// 
        /// </summary>
        /// <param name="retrieval"></param>
        /// <returns></returns>
        OperateResult AppendRetrieval(Retrieval retrieval);


        /// <summary>
        /// 
        /// </summary>
        /// <param name="drsName"></param>
        /// <param name="resourceName"></param>
        /// <returns></returns>
        OperateResult RemoveResource(string drsName, string resourceName);


        /// <summary>
        /// 
        /// </summary>
        /// <param name="drsName"></param>
        /// <returns></returns>
        OperateResult RemoveRetrieval(string drsName);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ctxName"></param>
        void AddCtx(string ctxName);

          /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="match"></param>
        /// <returns></returns>
         IList<T> Gets<T>(Expression<Func<T, bool>> match, int start, int length, out int count, Expression<Func<T, bool>> order = null,
            bool loadLazy = false, string tableName = null, string ctxName = null) where T : IView, new();

         int InsertRec(string tableName, string content, bool isUnicode);

        bool UpdateRec(string sysid, string tableName, string content, bool isUnicode);

         HS_TABLE_FIELD GetFieldInfo(string tableName, string fieldName);

        byte[] GetBinary(string id);

        bool SaveFile(string srcFile, string targetFile);

        bool DeleteFile(string filePath);
    }
}
